package com.jdew.thkj.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.jdew.thkj.R;
import com.jdew.thkj.base.BaseActivity;
import com.jdew.thkj.manager.AppConfig;
import com.jdew.thkj.network.RetrofitManager;
import com.jdew.thkj.network.api.RequestApi;
import com.jdew.thkj.utils.MD5Utils;
import com.jdew.thkj.utils.RegexValidateUtils;
import com.jdew.thkj.utils.ToastUtils;

import butterknife.Bind;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yangzai on 2018/3/15 0015.
 */

public class CommonActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.sure)
    TextView sure;
    @Bind(R.id.ed_nick)
    EditText edNick;


    @Override
    public int bindLayout() {
        return R.layout.activity_common;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back: {
                finish();
            }
            break;
            case R.id.sure: {
                GetNickName();
            }
            break;
            default:
                break;
        }
    }

    public void GetNickName() {
        String timesamp= RegexValidateUtils.getTime13();
        String numsamp=RegexValidateUtils.getRandom10();
        RetrofitManager.getInstance(AppConfig.BASE_URL).createReq(RequestApi.class).SetNickName(edNick.getText().toString().trim(),sp.getString("userPhone",""), RegexValidateUtils.getUniquePsuedoID(),timesamp,numsamp,MD5Utils.getMD5(RegexValidateUtils.ASCIISort(timesamp+numsamp+RegexValidateUtils.getUniquePsuedoID()+sp.getString("SignToken","")+"NickName"+edNick.getText().toString().trim()+"UserPhone"+sp.getString("userPhone",""))).toUpperCase()).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<JSONObject>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onNext(JSONObject obj) {
                if (obj.getString("code").equals("0")) {
                    ToastUtils.getInstance(CommonActivity.this).showMessage("昵称修改成功");
                    Intent in=new Intent();
                    in.putExtra("nickName",edNick.getText().toString());
                    setResult(RESULT_OK,in);
                    finish();
                }else{
                    ToastUtils.getInstance(CommonActivity.this).showMessage("修改失败");
                }
            }
        });
    }
    @Override
    public void initView(View view) {
        sp = getSharedPreferences("user", Activity.MODE_PRIVATE);
        editor = sp.edit();
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(this);
        sure.setOnClickListener(this);
        title.setText("昵称");
    }

}
